<template>
  <div class="itemWrap">
    <!-- 全局属性 -->
    <AttrCfg @filedChange="filedChange" :form="attrForm" type="LIST"></AttrCfg>
    <!-- 字段配置 -->
    <FiledCfg type="LIST" :entFiledsList="entFiledsList" :dataSource="filedList"></FiledCfg>
  </div>
</template>

<script>
export default {
  props: {
    type: {
      type: String,
      default: 'new'
    }
  },
  data() {
    return {
      entFiledsList: [], // 企业表字段列表
      // 默认选项
      attrForm: {},
      filedList: [],
      tempObj: {} // 选中表对象
    }
  },
  components: {
    AttrCfg: () => import('@/views/businessAssembly/jsonComponent/base/attrCfg.vue'),
    FiledCfg: () => import('@/views/businessAssembly/jsonComponent/base/filedCfg.vue')
  },
  mounted() {
    if (this.type == 'new') {
      this.attrForm = {
        apltable: undefined,
        title: '列表模态框',
        table: undefined,
        yjctable: undefined,
        type: 'single',
        lookname: '',
        tname: '',
        ischoice: 'false',
        ismultiple: undefined,
        isaddbtn: 'false',
        params: {},
        paramsList: [],
        filter: {},
        fiterList: []
      }
      this.filedList = []
    } else {
      this.attrForm = this.$store.state.formJson.formObj.LIST[this.type].attrForm
      this.filedList = this.$store.state.formJson.formObj.LIST[this.type].filedList
    }
    if (this.$store.state.formJson.listData?.length) {
      this.$store.state.formJson.listData.forEach(v => {
        this.tempObj[v.title] = v.title
      })
    }
  },
  methods: {
    // 字段改变
    filedChange(list, type, tableName) {
      if (type) {
        let listCopy = list.map(item => {
          return {
            t: item.fieldname,
            b: '',
            mapping: 'fill',
            hidden: 'false',
            describe: item.comments,
            isparam: 'false',
            sort: null,
            rules: []
          }
        })
        if (listCopy?.length) {
          listCopy.forEach(v => {
            let temp = tableName + '#' + v.t
            if (temp in this.tempObj) {
              v.mapping = temp
            }
          })
        }
        this.filedList = listCopy
      } else {
        let arry = []
        if (list?.length) {
          arry = list.map(item => {
            return {
              title: `${item.fieldname} - ${item.comments}`,
              value: item.fieldname
            }
          })
        }
        this.entFiledsList = arry
      }
    }
  }
}
</script>

<style lang="less" scoped>
.itemWrap {
  height: 570px;
  overflow-y: auto;
}
</style>
